/* eslint no-unused-vars:0 */
import React, { Component } from 'react';
import PubSub from 'pubsub-js';

import { Pagination } from 'antd';
import { Modal, Button } from 'antd';

import PDF from 'react-pdf-js';

class PDFViewer extends Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        PubSub.subscribe(
            'readPdf',
            function(topic, message) {
                this.setState({
                    visible: true,
                    pdf:
                        localStorage.getItem('url') +
                        '/report/onlineView/' +
                        message
                });
                console.log(123);
            }.bind(this)
        );
    }

    state = {
        visible: false,
        page: 1,
        current: 1,
        total: 1,
        pdf: ''
    };

    onDocumentComplete = (totalPage) => {
        this.setState({ total: totalPage });
    };

    onChange(page) {
        this.setState({
            page: page
        });
    }

    pdfChange = (page) => {
        this.setState({
            page: page,
            current: page
        });
    };

    handleOk = (e) => {
        this.setState({
            visible: false,
            pdf: ''
        });
    };

    render() {
        return (
            <Modal
                title="修改信息"
                visible={this.state.visible}
                okText="确定"
                cancelText="取消"
                onOk={this.handleOk}
                onCancel={this.handleOk}
                width="900px">
                <div style={{ textAlign: 'center' }}>
                    {this.state.pdf == '' ? (
                        <div />
                    ) : (
                        <div>
                            <PDF
                                file={this.state.pdf}
                                onDocumentComplete={this.onDocumentComplete}
                                page={this.state.page}
                            />

                            <Pagination
                                onChange={this.pdfChange}
                                current={this.state.current}
                                pageSize={1}
                                total={this.state.total}
                            />
                        </div>
                    )}
                </div>
            </Modal>
        );
    }
}

export default PDFViewer;
